<?php

namespace Home\Controller;

class RewardController extends BaseController {

    public function giveMe() {

        $where['d.user_id'] = $this->user_id;

        $list = M()
                ->table('ot_reward r')
                ->field('u.user_id,u.head_imgurl,u.nick_name,d.dog_name,r.number')
                ->join('ot_users u on r.user_id = u.user_id')
                ->join('ot_dog d on r.dog_id=d.dog_id')
                ->where($where)
                ->select();
        if (empty($list)) {
            $list = [];
        }
        $this->success('获取成功', $list);
    }

    public function myReward() {
        $p = I('p', 0, 'intval');
        $size = I('psize', 15, 'intval');
        if ($p) {
            $page = $p * $size;
        }

        $where['r.user_id'] = $this->user_id;

        $list = M()
                ->table('ot_reward r')
                ->field('d.dog_id,d.dog_name,d.dog_face,r.number,r.create_time')
                ->join('ot_dog d on r.dog_id=d.dog_id')
                ->where($where)
                ->limit($page, $size)
                ->order('r.create_time desc')
                ->select();

        $new_list = [];

        if (!empty($list)) {
            $tmp = [];


            $tmp_time = '';
            $time = '';
            foreach ($list as $val) {
                $create_time = strtotime($val['create_time']);
                if (empty($tmp_time) && empty($time)) {
                    $time = date('Y-m-d', $create_time);
                    $tmp_time = date('Y-m-d', $create_time);
                }

                $time = date('Y-m-d', $create_time);

//                echo $time . '#';
//                echo $tmp_time . '+';

                if ($time != $tmp_time) {
                    $new_list[] = $tmp;
                    $tmp = [];
                }
                $val['dog_face'] = 'https://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $val['dog_face'];
                $tmp[$time]['date_time'] = $time;
                $tmp[$time]['value'][] = $val;
                $tmp_time = $time;
            }

            $new_list[] = $tmp;
        }

//        var_dump($new_list);die;

        $result_list = [];
        foreach ($new_list as $row) {
            foreach ($row as $val) {
                $result_list[] = $val;
                break;
            }
//            var_dump($row);die;
        }
        $this->success('获取成功', $result_list);
    }

}
